#!/usr/bin/env python
# -*- coding:utf-8 -*-

import MySQLdb
from mylog import MyLog


class SaveToMySQL(object):
    def __init__(self, items):
        self.log = MyLog()
        self.host = '127.0.0.1'
        self.port = 3306
        self.user = 'root'
        self.passwd = 'xhq021620'
        self.db = 'spider_data'

        self.run(items)


    def run(self, items):
        conn = MySQLdb.connect(
        host = self.host,
        port = self.port,
        user = self.user,
        passwd = self.passwd,
        db = self.db,
        charset = 'utf8'
        )
        cur = conn.cursor()

        for item in items:
            cur.execute('''insert into qidian_books (name, author, category, word_num, introduction, url) values (%s, %s, %s, %s, %s, %s)''', (item.name, item.author,
            item.category, item.word_num, item.introduction, item.url))

        conn.commit()

        cur.close()
        conn.close()

        self.log.info('Write To MySQL spider_data.qidian_book.db Success')
